package com.example.excise06.service.impl;

import com.example.excise06.repository.RoommateRepository;
import com.example.excise06.service.AiFaceService;
import com.example.excise06.service.RoommateService;
import com.example.excise06.service.dto.FaceItem;
import com.example.excise06.service.dto.Roommate;
import com.example.excise06.service.dto.RoommateFaceDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoommateServiceImpl implements RoommateService {
    @Resource
    private AiFaceService aiFaceService;
    @Resource
    private RoommateRepository roommateRepository;

    @Override
    public List<RoommateFaceDTO> getFaces(String imageBase64) {
        List<FaceItem> faceItems = aiFaceService.detectImageBase64(imageBase64, "10");
        if (faceItems != null) {
            //定义最终结果集
            List<RoommateFaceDTO> result = new ArrayList<>();
            //逐个人脸进行识别处理和档案信息查询
            for (FaceItem faceItem : faceItems) {
                //使用人脸检测到的人脸编码进行人脸识别，获得识别结果
                String faceStudentCode = this.aiFaceService.searchFace(faceItem.getFaceToken());
                //正确识别到用户号：在创建人脸时，使用学号创建
                if (faceStudentCode != null) {
                    Roommate roommate = roommateRepository.findByStudentCode(faceStudentCode);
                    if (roommate != null) {//可以找到寝室档案信息
                        RoommateFaceDTO roommateFaceDTO = new RoommateFaceDTO(roommate, faceItem);
                        result.add(roommateFaceDTO);
                        continue;
                    }
                }
                //其他未找到请示档案的情况，只赋值人脸检测结果
                RoommateFaceDTO roommateFaceDTO = new RoommateFaceDTO();
                roommateFaceDTO.setFaceItem(faceItem);
                roommateFaceDTO.setSaySomething("没有找到这张脸");
                roommateFaceDTO.setRoomNickname("未知人员");
                result.add(roommateFaceDTO);
            }
            return result;
        }
        return null;
    }
}